<?php
namespace app\forum\model;
use app\base\model\BaseModel;
/**
 * 回复操作
 */
class ReplyModel extends BaseModel {
    //完成
    protected $_auto = array (
        //全部
        array('thread_id','intval',3,'function'), //帖子ID
        array('time','time',1,'function'), //时间
        array('user_id',USER_ID,1,'string'), //回复用户
     );
    //验证
    protected $_validate = array(
        array('content','require', '请填写回复内容', 1 , 'regex'),
        array('thread_id','require', '帖子ID获取不正确', 1 ,'regex'),
    );

    /**
     * 获取列表
     * @return array 列表
     */
    public function loadList($where = array(), $limit = 50, $order = 'A.time desc,A.reply_id desc'){
        $where['B.status'] = 1;
        $pageList = $this->table("reply as A")
                    ->join('{pre}user as B ON A.user_id = B.user_id')
                    ->field('A.*,B.username')
                    ->where($where)
                    ->limit($limit)
                    ->order($order)
                    ->select();
        //处理数据类型
        $list=array();
        if(!empty($pageList)){
            $i = 0;
            foreach ($pageList as $key=>$value) {
                $list[$key]=$value;
                $list[$key]['content'] = \app\forum\util\Ubb::ubbToHtml($value['content']);
                $list[$key]['i'] = $i++;
            }
        }
        return $list;
    }

    /**
     * 获取数量
     * @return int 数量
     */
    public function countList($where = array()){
        return $this->table("reply as A")
                    ->join('{pre}user as B ON A.user_id = B.user_id')
                    ->where($where)
                    ->count();
    }

    /**
     * 获取带回复主题列表
     * @return array 列表
     */
    public function loadThreadList($where = array(), $limit = 50, $order = 'A.time desc,A.reply_id desc'){
        $pageList = $this->table("reply as A")
                    ->join('{pre}thread as B ON A.thread_id = B.thread_id')
                    ->join('{pre}plate as C ON B.plate_id = C.plate_id')
                    ->join('{pre}user as D ON B.post_user = D.user_id')
                    ->field('A.*,B.title,C.name as class_name,C.app,C.parent_id,D.username as post_username')
                    ->where($where)
                    ->limit($limit)
                    ->order($order)
                    ->select();
        //处理数据类型
        $list=array();
        if(!empty($pageList)){
            $i = 0;
            foreach ($pageList as $key=>$value) {
                $list[$key]=$value;
                $list[$key]['content'] = get_text_make(\app\forum\util\Ubb::ubbToHtml($value['content']),500);
                $list[$key]['aurl'] = target('forum/Thread')->getUrl($value);
                $list[$key]['i'] = $i++;
            }
        }
        return $list;
    }
    
    /**
     * 获取信息
     * @param array $where 条件
     * @return array 信息
     */
    public function getWhereInfo($where){
        $where['B.status'] = 1;
        $info = $this->table("reply as A")
                    ->join('{pre}user as B ON A.user_id = B.user_id')
                    ->field('A.*,B.username')
                    ->where($where)
                    ->find();
        if(!empty($info)){
            $info['app'] = strtolower($info['app']);
        }
        return $info;
    }

    /**
     * 更新信息
     * @param string $type 更新类型
     * @return bool 更新状态
     */
    public function saveData($type = 'add'){
        $data = $this->create();
        if(!$data){
            return false;
        }
        if($type == 'add'){
            //回复间隔
            $where = array();
            $where['user_id'] = USER_ID;
            $info = $this->where($where)->order('reply_id desc')->find();
            if($info){
                if($info['content'] == $data['content']){
                    $this->error = '请不要发布重复内容！';
                    return false;
                }
                if(($info['time']+20) > time()){
                    $this->error = '抱歉，20秒内禁止再次回帖，请稍等！';
                    return false;
                }
            }
            //保存基本信息
            $status = $this->add($data);
            if(!$status){
                return false;
            }
            //更新主贴信息
            $info = target('forum/Thread')->getInfo($data['thread_id']);
            $array = array();
            $array['thread_id'] = $data['thread_id'];
            $array['reply_user'] = $data['user_id'];
            $array['reply_time'] = $data['time'];
            $array['reply_num'] = $info['reply_num'] + 1;
            target('forum/Thread')->save($array);
            return $status;
        }
        if($type == 'edit'){
            if(empty($data['reply_id'])){
                return false;
            }
            $status = $this->save();
            if($status === false){
                return false;
            }else{
                return true;
            }
        }
        return false;
    }

    /**
     * 修改信息
     * @param string $type 更新类型
     * @return bool 更新状态
     */
    public function editData($data){
        $map = array();
        $map['reply_id'] = $data['reply_id'];
        return $this->where($map)->data($data)->save();
    }

    /**
     * 删除信息
     * @param int $replyId ID
     * @return bool 删除状态
     */
    public function delData($replyId){
        $map = array();
        $map['reply_id'] = $plateId;
        return $this->where($map)->delete();
    }

}
